package com.study.springboottest.util;

import com.study.springboottest.util.annotations.CopySourceName;
import org.apache.commons.lang.ClassUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;
import org.springframework.lang.Nullable;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class BeanCopyUtils {

    public static <T> T copyProperties(Object source, T target) {
        validate(source, target);
        copyProperties(source, target, (String[]) null);
        return target;
    }

    public static <T> T copyProperties(Object source, Class<T> target) {
        T t = getInstance(target);
        return copyProperties(source, t);
    }

    public static <T> List<T> copyListProperties(List<?> source, Class<T> target) {
        Objects.requireNonNull(source, "source can not be null!");
        return source.stream().map(sou -> copyProperties(sou, target)).collect(Collectors.toList());
    }

    private static <T> T getInstance(Class<T> clazz) {
        try {
            Constructor<T> constructor = clazz.getDeclaredConstructor();
            if (!constructor.isAccessible()) {
                constructor.setAccessible(true);
            }
            return constructor.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(clazz.getName() + "must have non arg constructor!", e);
        }
    }

    private static void validate(Object source, Object target) {
        Objects.requireNonNull(source, "source can not be null!");
        Objects.requireNonNull(target, "target can not be null!");
    }

    private static void copyProperties(Object source, Object target, @Nullable String... ignoreProperties) {
        Objects.requireNonNull(source, "source can not be null!");
        Objects.requireNonNull(target, "target can not be null!");
        Class<?> actualEditable = target.getClass();

        PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable);
        List<String> ignoreList = ignoreProperties != null ? Arrays.asList(ignoreProperties) : null;
        int var8 = targetPds.length;
        HashMap<String, String> copySourceNameMap = new HashMap<>();
        for (Field field : actualEditable.getDeclaredFields()) {
            CopySourceName annotation = field.getAnnotation(CopySourceName.class);
            if (annotation != null) {
                copySourceNameMap.put(field.getName(), annotation.source());
            }
        }
        for (int var9 = 0; var9 < var8; ++var9) {
            PropertyDescriptor targetPd = targetPds[var9];
            Method writeMethod = targetPd.getWriteMethod();
            String name = targetPd.getName();
            String sourceFieldName = copySourceNameMap.get(name);
            if (null != sourceFieldName) {
                name = sourceFieldName;
            }
            if (writeMethod != null && (ignoreList == null || !ignoreList.contains(name))) {
                PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(source.getClass(), name);
                if (null != sourcePd) {
                    Method readMethod = sourcePd.getReadMethod();
                    if (null != readMethod && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
                        try {
                            if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                                readMethod.setAccessible(true);
                            }
                            Object value = readMethod.invoke(source);
                            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }
                            writeMethod.invoke(target, value);
                        } catch (Throwable var15) {
                            throw new FatalBeanException("Could not copyproperty '" + targetPd.getName() + "' from source to target", var15);
                        }
                    }
                }
            }
        }
    }
}
